package test;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.Payload;
import javax.validation.constraints.NotNull;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.labofjet.bean.impl.ValidationResult;
import com.labofjet.util.MessageUtils;
import com.labofjet.util.ValidationUtils;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/spring/application-context.xml")
public class ValidationUtilsTest1 {

	@Test
	public void testValidate() {
		VDTO v = new VDTO();
		ValidationResult validateObj = ValidationUtils.validateObj(v);
		System.out.println(validateObj);
	}
}

@ValidateA
class VDTO {
	//@NotNull(message = "a不能是空的")
	//@NotNull
	Integer a;
}

@Target({ ElementType.TYPE, ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = { AValidator.class })
@Documented
@interface ValidateA {
	String message() default "社保经办机构为{0}、社保编码为{1}，参保费种为{2}、品目为{3}，子目为{4}的参保明细存在重复记录，请修改。!";

	Class<?>[] groups() default {};

	Class<? extends Payload>[] payload() default {};
}

class AValidator implements ConstraintValidator<ValidateA, VDTO> {

	@Override
	public void initialize(ValidateA constraintAnnotation) {
	}

	@Override
	public boolean isValid(VDTO v, ConstraintValidatorContext context) {
		String message = "{0}&{1}";
		context.disableDefaultConstraintViolation();
		context.buildConstraintViolationWithTemplate(
				MessageUtils.getMessage("0", message, new String[] { "占位符1", "占位符2" })).addConstraintViolation();
		return false;
	}
}